package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.adapter.DispatchPatrolOrderAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.Dispatchbean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 工程 工单管理 分派工单 工程施工
 * Created by tony on 2018/1/31.
 */

public class DispatchEngineeringConstructOrderActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ImageView img_back;
    private TextView tv_title;
    private RelativeLayout rl_engineering_name;
    private ImageView img_check_dispatch_information, img_edit_dispatch_information;
    private TextView tv_project_name,tv_engineering_name,edt_dispatching_way_colon,edt_receive_colon,edt_external_dispatch_people,edt_external_dispatch_time;
    private TextView btn_confirm_the_dispatch,btn_confirm_detele;
    private ListView lv;
    private ImageView img_construct_select;
    private DispatchPatrolOrderAdapter adapter;
    private List<Dispatchbean> datas = new ArrayList<>();
    private final String TAG = DispatchEngineeringConstructOrderActivity.class.getSimpleName();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_dispatch_engineering_construct_order);

        init();
    }

    private void init() {
        img_back = findViewById(R.id.img_back);
        tv_title = findViewById(R.id.tv_title);
        rl_engineering_name=findViewById(R.id.rl_engineering_name);
        tv_project_name=findViewById(R.id.tv_project_name);
        tv_engineering_name=findViewById(R.id.tv_engineering_name);
        edt_dispatching_way_colon=findViewById(R.id.edt_dispatching_way_colon);
        edt_receive_colon=findViewById(R.id.edt_receive_colon);
        edt_external_dispatch_people=findViewById(R.id.edt_external_dispatch_people);
        edt_external_dispatch_time=findViewById(R.id.edt_external_dispatch_time);
        img_check_dispatch_information = findViewById(R.id.img_check_dispatch_information);
        img_edit_dispatch_information = findViewById(R.id.img_edit_dispatch_information);
        img_construct_select=findViewById(R.id.img_construct_select);
        lv = findViewById(R.id.lv);
        btn_confirm_detele=findViewById(R.id.btn_confirm_detele);


        tv_title.setText(getResources().getText(R.string.engineering_construct));
        img_construct_select.setBackgroundResource(R.mipmap.arrow_down_icon);

        img_back.setOnClickListener(this);
        rl_engineering_name.setOnClickListener(this);
        img_check_dispatch_information.setOnClickListener(this);
        img_edit_dispatch_information.setOnClickListener(this);
        btn_confirm_detele.setOnClickListener(this);
        img_construct_select.setOnClickListener(this);

        adapter = new DispatchPatrolOrderAdapter(this, TAG);
//        adapter.setData(datas);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent mIntent = new Intent(DispatchEngineeringConstructOrderActivity.this, ViewDispatchOrderInformationForEngineeringConstructActivity.class);
        mIntent.putExtra(ConstantUtil.KEY_1, i);
        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_2);
    }

    // 施工内容 收放
    private void showConstrctEngineeringContentSelect() {
        if (lv.getVisibility() == View.VISIBLE) {
            lv.setVisibility(View.GONE);
            img_construct_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            lv.setVisibility(View.VISIBLE);
            img_construct_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            // 返回
            case R.id.img_back:
                finish();
                break;
                // 项目名称/工程名称
            case R.id.rl_engineering_name:
                mIntent = new Intent(DispatchEngineeringConstructOrderActivity.this, ViewEngineeringConstructOrderDetailActivity.class);
                startActivity(new Intent(mIntent));
                break;
            case R.id.img_check_dispatch_information:
                mIntent = new Intent(DispatchEngineeringConstructOrderActivity.this, ViewConstructContentActivity.class);
                startActivity(mIntent);
                break;
                // 派工信息
            case R.id.img_edit_dispatch_information:
               // mIntent = new Intent(DispatchEngineeringConstructOrderActivity.this, EditConstructOrderDispatchInfomationActivity.class);
                mIntent = new Intent(DispatchEngineeringConstructOrderActivity.this, EditDispatchInformationForEngineeringInstallationOrderActivity.class);
                break;
                // 施工内容
            case R.id.img_construct_select:
                showConstrctEngineeringContentSelect();
                break;
                // 确认派工
            case R.id.btn_confirm_the_dispatch:
                break;
                // 删除工单
            case R.id.btn_confirm_detele:
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
//                    datas.add(new EngineeringOrderBean("", "", "", "", ConstantUtil.WATTING_EDIT));
//                    adapter.setData(datas);
//                    adapter.notifyDataSetChanged();
                }
                break;
            case ConstantUtil.START_ACTIVITY_KEY_2:
                if (data != null) {
                    String str = data.getStringExtra(ConstantUtil.KEY_1);
                    int position = data.getIntExtra(ConstantUtil.KEY_2, -1);
                    if ("dispatch".equals(str)) {

                    } else if ("delete".equals(str)) {
                        if (position >= 0) {
                            datas.remove(position);
//                            adapter.setData(datas);
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
                break;
//            case ConstantUtil.ADD_PRODUCT_INFOMATION:
//                if (data != null) {
//                    List<EngineeringConstructContentBean> dataLs = (List<EngineeringConstructContentBean>) data.getSerializableExtra(ConstantUtil.PRODUCTBEAN);
//                    datas = dataLs;
//                    adapter.setData(datas);
//                    adapter.notifyDataSetChanged();
//                }
//                break;
//            case ConstantUtil.ADD_PRODUCT_LIST:
//                if (data != null) {
//                    Bundle mBundle = data.getExtras();
//                    if (mBundle != null) {
//                        String start_date = mBundle.getString(ConstantUtil.KEY_1);
//                        String end_date = mBundle.getString(ConstantUtil.KEY_2);
//                        EngineeringConstructContentBean bean = datas.get(selectedPosition);
//                        bean.setStartDate(start_date);
//                        bean.setEndDate(end_date);
//                        adapter.setData(datas);
//                        adapter.notifyDataSetChanged();
//                    }
//                }
//                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
